import { defineStore, } from 'pinia'
import { reqGetCategory1, reqGetCategory2, reqGetCategory3 } from '@/api/product/category'
import type { cateIdModel, categoryListModel } from '@/api/product/model/category'
// pinia 管理的分类状态
export interface categoryStateModel extends cateIdModel {
  cate1List: categoryListModel,
  cate2List: categoryListModel,
  cate3List: categoryListModel,
}

export const useCategoryStore = defineStore('category', {
  state: (): categoryStateModel => {
    return {
      cate1List: [],
      cate2List: [],
      cate3List: [],
      cate1Id: '',
      cate2Id: '',
      cate3Id: ''
    }
  },
  actions: {
    async setCategory1List() {
      this.cate1List = await reqGetCategory1()
    },
    async setCategory2List(category1Id: number) {
      this.cate1Id = category1Id
      this.cate2List = await reqGetCategory2(category1Id)
      // 清空数据
      this.cate2Id = ''
      this.cate3Id = ''
      this.cate3List = []
    },
    async setCategory3List(category2Id: number) {
      this.cate2Id = category2Id
      this.cate3List = await reqGetCategory3(category2Id)
      // 清空数据
      this.cate3Id = ''
    },
    setCate3Id(category3Id: number) {
      this.cate3Id = category3Id
    }
  },
  getters: {

  }
})